from typing import Any, Optional
from pydantic import BaseModel


class JsonResponse(BaseModel):
    """统一的JSON响应封装类"""
    code: int
    msg: str
    data: Optional[Any] = None

    @staticmethod
    def success(data: Any = None, msg: str = "success") -> "JsonResponse":
        """成功响应
        
        Args:
            data: 响应数据
            msg: 响应消息
            
        Returns:
            JsonResponse: 成功响应对象
        """
        return JsonResponse(code=200, msg=msg, data=data)

    @staticmethod
    def error(msg: str = "error", code: int = 500) -> "JsonResponse":
        """错误响应
        
        Args:
            msg: 错误消息
            code: 错误码
            
        Returns:
            JsonResponse: 错误响应对象
        """
        return JsonResponse(code=code, msg=msg, data=None)